from django.db import models
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.models import CoreModel, SoftDeleteModel
from dvadmin.utils.viewset import CustomModelViewSet
from rest_framework.viewsets import ModelViewSet

# drf专用过滤器模块
from django_filters import rest_framework as filters


class Station(CoreModel):

    station = models.TextField(verbose_name="station", blank=True, null=True)
    gong_wei_ming_cheng = models.TextField(verbose_name="工位名称", blank=True, null=True)
    sheng_chan_xian = models.TextField(verbose_name="生产线", blank=True, null=True)

    class Meta:
        db_table = 'station_station'


class StationModelSerializer(CustomModelSerializer):    
    class Meta:
        model = Station
        fields = '__all__'


class StationFilter(filters.FilterSet):
    station = filters.CharFilter(field_name='station', lookup_expr='icontains')
    gong_wei_ming_cheng = filters.CharFilter(field_name='gong_wei_ming_cheng', lookup_expr='icontains')
    sheng_chan_xian = filters.CharFilter(field_name='sheng_chan_xian', lookup_expr='icontains')
    class Meta:
        models = Station
        fields = '__all__'


class StationViewSet(CustomModelViewSet):
    # FIXME: 把extra_filter_class = []放到这里，避免更新数据时权限不够错误
    # extra_filter_class = []
    # permission_classes = []
    values_queryset= Station.objects.all()
    serializer_class = StationModelSerializer
    # FIXME 用django-filter的filter_class
    filter_class = StationFilter
    # 下面的filterset_class 也可以实现上面的功能
    # filterset_class = StationFilter
    # [ ]: 是否可以不用filter_class，直接用search_fields
    # search_fields = ['station']
    # filterset_fields = ['station']

